{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "850743ad-13a5-4b37-baa0-d6283c71a02a",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'jieba'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[19], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mjieba\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m cut \n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mre\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m sub\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mgetwords\u001b[39m(file,stopList):\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'jieba'"
     ]
    }
   ],
   "source": [
    "from jieba import cut \n",
    "from re import sub\n",
    "def getwords(file,stopList):\n",
    "    wordsList=[]\n",
    "    for line in open(file,encoding='utf-8'):\n",
    "        line=line.strip()\n",
    "        line=sub(r'[.【】0-9、--，。！\\~*]','',line)\n",
    "        line=cut(line)\n",
    "        line=filter(lambda word:len(word)>1,line)\n",
    "        wordsList.extend(line)\n",
    "        words=[]\n",
    "        for i in wordsList:\n",
    "            if i not in stopList and i.sstrip()!='' and i!=None:\n",
    "                words.append(i)\n",
    "return words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "93ec2836-eb9e-4be6-893e-8ace644e7cc1",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "unterminated string literal (detected at line 7) (1392338274.py, line 7)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[12], line 7\u001b[1;36m\u001b[0m\n\u001b[1;33m    line=sub(r'[.【】0-9、--，。! \\ ~ *,'',line)\u001b[0m\n\u001b[1;37m                                    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unterminated string literal (detected at line 7)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from collections import Counter\n",
    "from itertools import chain\n",
    "allwords=[]\n",
    "for spamfile in spamFileList:\n",
    "    words=getWords(\".//item5/item5-ss-data/spam/\"+spamfile,stopList)\n",
    "    allwords.append(words)\n",
    "for normalfile in normalFileList:\n",
    "    words=getWords(\".//item5/item5-ss-data/spam/\"+spamfile,stopList)\n",
    "    allwords.append(words)\n",
    "print('训练集所有的有效词语类表')\n",
    "print(allwords)\n",
    "frep=Counter(chain(*allwords))\n",
    "topTen=frep.most_common(10)\n",
    "topwords=[w[0] for w in topTen]\n",
    "print(\"训练集中出现频次最高的10个词语:\")\n",
    "print(topwords)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "06d090cd-b452-46f0-958b-e069fefb4c20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10个高频词语在每封邮件中出现的次数:\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "vector=[]\n",
    "for words in allwords:\n",
    "    temp=list(map(lambda x;words.count(x),topWords))\n",
    "    vector.append(temp)\n",
    "vector=np.array(vector)\n",
    "print(\"10个高频词语在每封邮件中出现的次数:\")\n",
    "print(vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83a811a9-6140-46c9-aa70-bdde69c23a60",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
